'''

Case Type： 数据库系统
Case Name： 将pg_log目录权限删除，重启数据库失败
Case No.    Opengauss_Reliability_Dbsys_Case042
Create At:  2020/06/30
@zou_jialiang0505328126
Descption:  1.删除pg_log权限2.	重启数据库

history：
created： 2020/06/30  created by @wan005
modified: 2020/08/11  不生成core文件，对应用例已修改
'''
import os
import unittest
from yat.test import Node
import time
import _thread
import queue
from yat.test import macro

import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class ModifyPglogPermi(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case042.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.COREFILE_PATH = macro.COREFILE_PATH
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.com = Common()

    def test_modify_pglog_permission(self):
        # delete pg_log permission
        self.log.info("----------------delete pg_log permission------------------")
        modfiy_permis_cmd = 'chmod -R 000 ' + self.PG_LOG_PATH
        self.log.info(modfiy_permis_cmd)
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.assertEqual(modify_msg, '')

        # restart gaussDB fail
        is_stop = self.sh.stopDbCluster()
        self.log.info(is_stop)
        self.assertTrue(is_stop)
        is_started = self.sh.startDbCluster()
        self.log.info(is_started)
        self.assertFalse(is_started)

    def tearDown(self):
        self.log.info('-------------this is tearDown----------------')
        # recover pg_log permission
        print('----------------recover pg_log permission-----------------')
        modfiy_permis_cmd = 'chmod -R 700 ' + self.PG_LOG_PATH
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.log.info(modfiy_permis_cmd)

        is_started = self.sh.startDbCluster()
